<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Database\Query\Builder;

class Order extends Model
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'order';
    public static $statusList = [
        1 => '待支付',
        2 => '已支付',
        3 => '取消支付',
        4 => '支付失败'
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function information()
    {
        return $this->belongsTo(Information::class, 'information_id');
    }

    public function getStatusTextAttribute()
    {
        return static::$statusList[$this->status] ?? '待支付';
    }

    /**
     * 通过订单号获取
     * @param $number
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public static function getByNumber($number)
    {
        return static::query()->where('number', $number)->first();
    }

    /**
     * 关键字搜索
     * @param $q
     * @param $keyword
     * @return Builder
     */
    public function scopeKeyword($q, $keyword)
    {
        /** @var Builder $q */
        return $q->where(function ($q) use ($keyword) {
            /** @var Builder $q */
            $q->where('number', 'like', '%' . $keyword . '%');
            $q->orWhereHas('user', function ($q) use ($keyword) {
                //                $q->where('nickname', 'like', '%' . $keyword . '%');
                /** @var Builder $q */
                $q->where('mobile', 'like', '%' . $keyword . '%');
            });
        });
    }

    /**
     * 设置信息内容
     * @param Information $information
     * @return $this
     */
    public function setInformation(Information $information)
    {
        $this->information_id = $information->id;
        $this->top_days = $information->top_days;

        //获取对应置顶天数的费用
        $topConfig = TopConfig::getByDays($information->top_days);
        $price = $topConfig->price ?? 0.00;
        if (!$topConfig) {
            $system = System::getById(1);
            $price = bcmul((string)$information->top_days, (string)($system->top_price ?? 0.00), 2);
        }

        $this->total = $price;

        return $this;
    }

    public function pay()
    {

    }

    /**
     * 创建订单号
     * @return $this
     */
    public function createNumber()
    {
        $num = 'WB' . time() . rand(10000, 99999);
        $order = static::getByNumber($num);
        if ($order) {
            $this->createNumber();
        }
        $this->number = $num;
        return $this;
    }

    /**
     * 取消订单
     * @return $this
     */
    public function cancel()
    {
        $this->status = 3;
        return $this;
    }

    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        /** @var Information $information */
        $information = $this->information;
        $infoObj = $information->getInfoObject();
        $data = ['id' => $this->id, 'number' => $this->number ?? '', 'status' => $this->status ?? '', 'status_text' => $this->status_text, 'img' => stripslashes(($information->imgs()->first()->url ?? '')), 'type' => $this->information->type ?? '', 'type_text' => $this->information->type_text ?? '', 'title' => $infoObj->title ?? $infoObj->content ?? '', 'top_days' => $this->top_days ?? 1, 'total' => (float)round($this->total ?? 0.0, 2), 'create_time' => $this->create_time->toDateTimeString()];
        if (in_array($information->type, [4, 5]) && $information->all_type) {
            $data['type_text'] .= '-' . $information->all_type_text;
        }
        return $data;
    }

    /**
     * 管理端接口返回
     * @return array
     */
    public function toAdminApi()
    {
        /** @var Information $information */
        $information = $this->information;
        $infoObj = $information->getInfoObject();
        $data = ['id' => $this->id, 'number' => $this->number ?? '', 'total' => $this->total ?? 0.0, 'top_days' => $this->top_days ?? '', 'status' => $this->status ?? '', 'status_text' => $this->status_text, 'pay_time' => $this->pay_time ?? '', 'pay_order' => $this->pay_order ?? '', 'user' => ['id' => $this->user_id, 'nickname' => $this->user->nickname ?? '', 'mobile' => $this->user->mobile ?? '', 'header' => stripslashes(($this->user->header->url ?? ''))], 'information' => ['id' => $this->information_id, 'title' => $infoObj->title ?? $infoObj->content ?? '', 'type' => $this->information->type ?? '', 'type_text' => $this->information->type_text ?? '', 'img' => $information->imgs()->first()->url ?? '']];
        if (in_array($information->type, [4, 5]) && $information->all_type) {
            $data['information']['type_text'] .= '-' . $information->all_type_text;
        }
        return $data;
    }
}